package org.zhouhe.modules.website;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.zhouhe.base.BaseAction;
import org.zhouhe.modules.article.service.ArticleService;
import org.zhouhe.page.PageUtil;
import org.zhouhe.vo.Article;
import org.zhouhe.vo.ArticleArticleType;
import org.zhouhe.vo.ArticleType;
import org.zhouhe.vo.Industry;

public class ArticleAction extends BaseAction {

	private static final long serialVersionUID = 896164905733014622L;
	private PageUtil pageUtil = new PageUtil();
	private Article article;
	private ArticleType articleType = new ArticleType();
	private Industry industry = new Industry();
	private Industry paramIndustry = new Industry();
	@Autowired
	private ArticleService articleService;

	public String list() {
		if (getPageUtil() == null)
			setPageUtil(new PageUtil());
		articleService.getArticleList(getPageUtil(), getArticle(), industry.getIndustryId(), articleType.getArticleTypeId());
		return "list";
	}

	public String detail() {
		Integer tid = articleType.getArticleTypeId();
		this.article = articleService.getArticleDetail(article);
		if(tid == null){
			this.articleType = article.getArticleArticleTypes().iterator().next().getArticleType();
		}else{
			this.articleType = articleService.getArticleType(tid);
		}
		this.setPageTitle(article.getTitle() +" - "+ articleType.getName());
		return "detail";
	}
	
	public String articleList4txix(){
		articleType = articleService.getArticleType(articleType.getArticleTypeId());
		this.pageUtil = articleService.getArticleList(pageUtil, article, paramIndustry.getIndustryId(), articleType.getArticleTypeId());
		
		this.setPageTitle(articleType.getName());
		if(articleType.getArticleType() != null){
			this.setPageTitle(this.getPageTitle() +" - "+ articleType.getArticleType().getName());
		}
		return "articleList4txix";
	}
	public String articleBlocks4t1ix(){
		articleType = articleService.getArticleType(articleType.getArticleTypeId());
		for(ArticleType t : articleType.getArticleTypes()){
			List<Article> lst = articleService.getArticleByType(t.getArticleTypeId(), industry.getIndustryId(), 12);
			Set<ArticleArticleType> set = new LinkedHashSet<ArticleArticleType>();
			for(Article atc : lst){
				ArticleArticleType aat = new ArticleArticleType();
				aat.setArticle(atc);
				set.add(aat);
			}
			t.setArticleArticleTypes(set);
		}
		this.setPageTitle(articleType.getName());
		return "articleBlocks4t1ix";
	}
	

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Article getArticle() {
		return article;
	}

	public void setIndustry(Industry industry) {
		this.industry = industry;
	}

	public Industry getIndustry() {
		return industry;
	}

	public void setArticleType(ArticleType articleType) {
		this.articleType = articleType;
	}

	public ArticleType getArticleType() {
		return articleType;
	}

	public void setParamIndustry(Industry paramIndustry) {
		this.paramIndustry = paramIndustry;
	}

	public Industry getParamIndustry() {
		return paramIndustry;
	}
}
